<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="ios-top-nav">
			<view class="status-bar"></view>
			<view class="nav-content">
				<view class="back-btn" @tap="goBack">
					<image src="/static/icons/back.png" mode="aspectFit"></image>
				</view>
				<text class="nav-title">作业上传</text>
				<view class="nav-actions">
					<view class="action-btn" @tap="submitHomework">
						<text>提交</text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 主要内容区域 -->
		<view class="main-content">
			<!-- 作业信息 -->
			<view class="homework-info">
				<view class="info-item">
					<text class="label">作业名称</text>
					<text class="value">{{homework.title}}</text>
				</view>
				<view class="info-item">
					<text class="label">所属课程</text>
					<text class="value">{{homework.courseName}}</text>
				</view>
				<view class="info-item">
					<text class="label">截止时间</text>
					<text class="value deadline">{{homework.deadline}}</text>
				</view>
			</view>
			
			<!-- 上传区域 -->
			<view class="upload-section">
				<view class="section-title">作业图片</view>
				<view class="upload-area" @tap="showUploadOptions" v-if="!imageInfo">
					<image src="/static/icons/camera.png" mode="aspectFit"></image>
					<text>点击拍照或选择图片</text>
					<text class="sub-text">支持拍照和从相册选择</text>
				</view>
				<view class="image-preview" v-else>
					<view class="image-container">
						<image :src="imageInfo.path" mode="aspectFit"></image>
						<view class="delete-btn" @tap="deleteImage">
							<image src="/static/icons/delete.png" mode="aspectFit"></image>
						</view>
					</view>
					<!-- 识别结果 -->
					<view class="recognition-result" v-if="recognitionText">
						<view class="result-header">
							<image src="/static/icons/ai.png" mode="aspectFit"></image>
							<text>AI识别结果</text>
						</view>
						<view class="result-content">
							<text>{{recognitionText}}</text>
						</view>
					</view>
					<!-- 识别中状态 -->
					<view class="recognition-loading" v-if="isRecognizing">
						<view class="loading-spinner"></view>
						<text>正在识别中...</text>
					</view>
				</view>
			</view>
			
			<!-- 备注信息 -->
			<view class="remark-section">
				<view class="section-title">备注说明</view>
				<textarea 
					class="remark-input"
					v-model="remark"
					placeholder="请输入作业说明（选填）"
					:maxlength="200"
					show-confirm-bar
				></textarea>
				<text class="word-count">{{remark.length}}/200</text>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			homework: {
				title: "第三章习题",
				courseName: "高等数学",
				deadline: "2023-06-10 23:59"
			},
			imageInfo: null,
			recognitionText: "",
			isRecognizing: false,
			remark: ""
		}
	},
	methods: {
		goBack() {
			uni.navigateBack();
		},
		showUploadOptions() {
			uni.showActionSheet({
				itemList: ['拍照', '从相册选择'],
				success: (res) => {
					if (res.tapIndex === 0) {
						this.takePhoto();
					} else {
						this.chooseImage();
					}
				}
			});
		},
		takePhoto() {
			uni.chooseImage({
				count: 1,
				sourceType: ['camera'],
				success: (res) => {
					this.handleImageSelected(res.tempFilePaths[0]);
				}
			});
		},
		chooseImage() {
			uni.chooseImage({
				count: 1,
				sourceType: ['album'],
				success: (res) => {
					this.handleImageSelected(res.tempFilePaths[0]);
				}
			});
		},
		handleImageSelected(path) {
			this.imageInfo = {
				path: path
			};
			this.startRecognition();
		},
		deleteImage() {
			this.imageInfo = null;
			this.recognitionText = "";
		},
		startRecognition() {
			this.isRecognizing = true;
			this.recognitionText = "";
			
			// 模拟图片识别过程
			setTimeout(() => {
				// 这里应该调用实际的图片识别API
				this.recognitionText = "这是一道关于微积分的题目，主要考察导数的概念和应用。题目要求计算函数f(x) = x² + 2x + 1在x = 1处的导数。";
				this.isRecognizing = false;
			}, 2000);
		},
		submitHomework() {
			if (!this.imageInfo) {
				uni.showToast({
					title: '请上传作业图片',
					icon: 'none'
				});
				return;
			}
			
			uni.showLoading({
				title: '正在提交...'
			});
			
			// 模拟提交过程
			setTimeout(() => {
				uni.hideLoading();
				uni.showToast({
					title: '提交成功',
					icon: 'success'
				});
				setTimeout(() => {
					uni.navigateBack();
				}, 1500);
			}, 2000);
		}
	}
}
</script>

<style lang="scss">
.container {
	height: 100vh;
	display: flex;
	flex-direction: column;
	background-color: var(--ios-light-bg, #F2F2F7);
}

/* iOS顶部导航栏 */
.ios-top-nav {
	background-color: rgba(255, 255, 255, 0.85);
	backdrop-filter: blur(10px);
	-webkit-backdrop-filter: blur(10px);
	border-bottom: 0.5px solid var(--ios-separator, rgba(60, 60, 67, 0.1));
	
	.status-bar {
		height: var(--status-bar-height, 20px);
	}
	
	.nav-content {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10px 16px;
		min-height: 44px;
	}
	
	.back-btn {
		width: 32px;
		height: 32px;
		display: flex;
		align-items: center;
		justify-content: center;
		
		image {
			width: 20px;
			height: 20px;
		}
	}
	
	.nav-title {
		font-size: 17px;
		font-weight: 600;
		color: #000;
	}
	
	.nav-actions {
		.action-btn {
			padding: 6px 12px;
			
			text {
				color: var(--ios-blue, #007AFF);
				font-size: 15px;
				font-weight: 500;
			}
		}
	}
}

/* 主内容区域 */
.main-content {
	flex: 1;
	padding: 16px;
	overflow-y: auto;
	-webkit-overflow-scrolling: touch;
}

/* 作业信息 */
.homework-info {
	background-color: var(--ios-card-bg, #FFFFFF);
	border-radius: var(--ios-border-radius, 10px);
	padding: 16px;
	margin-bottom: 16px;
	box-shadow: var(--ios-card-shadow, 0 1px 2px rgba(0, 0, 0, 0.05));
	
	.info-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 12px;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.label {
			font-size: 14px;
			color: #8e8e93;
		}
		
		.value {
			font-size: 14px;
			color: #000;
			font-weight: 500;
			
			&.deadline {
				color: #FF3B30;
			}
		}
	}
}

/* 上传区域 */
.upload-section {
	margin-bottom: 16px;
	
	.section-title {
		font-size: 15px;
		font-weight: 600;
		color: #000;
		margin-bottom: 12px;
	}
	
	.upload-area {
		background-color: var(--ios-card-bg, #FFFFFF);
		border-radius: var(--ios-border-radius, 10px);
		padding: 32px;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		border: 1px dashed rgba(0, 122, 255, 0.3);
		
		image {
			width: 48px;
			height: 48px;
			margin-bottom: 12px;
		}
		
		text {
			font-size: 15px;
			color: #000;
			margin-bottom: 4px;
		}
		
		.sub-text {
			font-size: 12px;
			color: #8e8e93;
		}
	}
	
	.image-preview {
		background-color: var(--ios-card-bg, #FFFFFF);
		border-radius: var(--ios-border-radius, 10px);
		padding: 16px;
		
		.image-container {
			position: relative;
			width: 100%;
			height: 200px;
			margin-bottom: 16px;
			
			image {
				width: 100%;
				height: 100%;
				object-fit: contain;
				border-radius: 8px;
			}
			
			.delete-btn {
				position: absolute;
				top: 8px;
				right: 8px;
				width: 32px;
				height: 32px;
				background-color: rgba(0, 0, 0, 0.5);
				border-radius: 16px;
				display: flex;
				align-items: center;
				justify-content: center;
				
				image {
					width: 20px;
					height: 20px;
					margin: 0;
				}
			}
		}
		
		.recognition-result {
			background-color: rgba(0, 122, 255, 0.05);
			border-radius: 8px;
			padding: 12px;
			
			.result-header {
				display: flex;
				align-items: center;
				margin-bottom: 8px;
				
				image {
					width: 20px;
					height: 20px;
					margin-right: 6px;
				}
				
				text {
					font-size: 14px;
					font-weight: 500;
					color: #007AFF;
				}
			}
			
			.result-content {
				font-size: 14px;
				line-height: 1.5;
				color: #333;
			}
		}
		
		.recognition-loading {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 20px;
			
			.loading-spinner {
				width: 30px;
				height: 30px;
				border: 3px solid rgba(0, 122, 255, 0.1);
				border-top-color: #007AFF;
				border-radius: 50%;
				margin-bottom: 12px;
				animation: spin 1s linear infinite;
			}
			
			text {
				font-size: 14px;
				color: #8e8e93;
			}
		}
	}
}

/* 备注信息 */
.remark-section {
	.section-title {
		font-size: 15px;
		font-weight: 600;
		color: #000;
		margin-bottom: 12px;
	}
	
	.remark-input {
		background-color: var(--ios-card-bg, #FFFFFF);
		border-radius: var(--ios-border-radius, 10px);
		padding: 12px;
		width: 100%;
		height: 120px;
		font-size: 14px;
		line-height: 1.5;
		color: #000;
		
		&::placeholder {
			color: #8e8e93;
		}
	}
	
	.word-count {
		text-align: right;
		font-size: 12px;
		color: #8e8e93;
		margin-top: 4px;
		display: block;
	}
}

@keyframes spin {
	to {
		transform: rotate(360deg);
	}
}

/* 暗黑模式适配 */
@media (prefers-color-scheme: dark) {
	.ios-top-nav {
		background-color: rgba(28, 28, 30, 0.85);
		border-bottom: 0.5px solid var(--ios-separator, rgba(255, 255, 255, 0.15));
		
		.nav-title {
			color: #FFFFFF;
		}
	}
	
	.homework-info {
		background-color: #2c2c2e;
		
		.info-item {
			.value {
				color: #FFFFFF;
			}
		}
	}
	
	.upload-section {
		.upload-area {
			background-color: #2c2c2e;
			border-color: rgba(0, 122, 255, 0.2);
			
			text {
				color: #FFFFFF;
			}
		}
		
		.image-preview {
			background-color: #2c2c2e;
			
			.recognition-result {
				background-color: rgba(0, 122, 255, 0.15);
				
				.result-content {
					color: #FFFFFF;
				}
			}
		}
	}
	
	.remark-section {
		.remark-input {
			background-color: #2c2c2e;
			color: #FFFFFF;
			
			&::placeholder {
				color: #8e8e93;
			}
		}
	}
}
</style> 